package com.hdyg.sixg.activity.trans;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.CertifAdapter;
import com.hdyg.sixg.entry.BuySellDetailBean;
import com.hdyg.sixg.mvp.Contrant.CSellDetail;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PSellDetail;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 我的出售详情
 * @time 2019/4/9 16:00
 */
public class SellDetailActivity extends BaseActivity implements CSellDetail.IVSellDetail {


    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_buy_1)
    SuperTextView svBuy1;
    @BindView(R.id.sv_buy_2)
    SuperTextView svBuy2;
    @BindView(R.id.sv_sell_3)
    SuperTextView svSell3;
    @BindView(R.id.sv_sell_4)
    SuperTextView svSell4;
    @BindView(R.id.sv_sell_5)
    SuperTextView svSell5;
    @BindView(R.id.sv_sell_6)
    SuperTextView svSell6;
    @BindView(R.id.sv_sell_7)
    SuperTextView svSell7;
    @BindView(R.id.sv_sell_8)
    SuperTextView svSell8;
    @BindView(R.id.sv_sell_9)
    SuperTextView svSell9;
    @BindView(R.id.sv_sell_10)
    SuperTextView svSell10;
    @BindView(R.id.sv_sell_11)
    SuperTextView svSell11;
    @BindView(R.id.sv_sell_12)
    SuperTextView svSell12;
    @BindView(R.id.sv_sell_13)
    SuperTextView svSell13;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CertifAdapter imageAdapter;
    private List<String> imageBeanList;
    private CSellDetail.IPSellDetail mPresenter;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sell_detail;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra(SpMsg.INTENT_STR);
        onClickListener();
        imageBeanList = new ArrayList<>();
        tvTopTitle.setText(R.string.detail_sell_title);
        rvImage.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        initAdapter();
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());

        //确认提交
        btSure.setOnClickListener(v -> {
            mPresenter.pGetSure(BaseUrl.CONFIR_ORDER_URL, GetParamUtil.getTradSureParam(id));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.SELL_DETAIL_URL, GetParamUtil.getSellDetailParam(id));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PSellDetail(this);
    }

    private void initAdapter() {
        imageAdapter = new CertifAdapter(R.layout.item_take_photo, imageBeanList);
        rvImage.setAdapter(imageAdapter);
        //删除图片
        imageAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int id = view.getId();
            if (id == R.id.ivDelete) {
                imageAdapter.remove(position);
            }
        });
    }

    private void setUI(String status) {
        switch (status) {
//            case "1":
//                ivAdd.setVisibility(View.VISIBLE);//显示上传按钮
//                btSure.setVisibility(View.VISIBLE);//显示确认订单
//                break;
            case "2":
//                ivAdd.setVisibility(View.GONE);//隐藏上传按钮
                btSure.setVisibility(View.VISIBLE);//显示确认订单
                break;
            case "3":
//                ivAdd.setVisibility(View.GONE);//隐藏上传按钮
                btSure.setVisibility(View.GONE);//隐藏确认订单
                break;
        }
    }

    @Override
    public void vGetInitSuccess(BuySellDetailBean dataBean) {
        if ("2".equals(dataBean.getType())) {
            setUI(dataBean.getStatus());
        }
        svBuy1.setRightString(dataBean.getText_status());//状态
        svBuy2.setRightString(dataBean.getUsername());//买方账号
        svSell3.setRightString(dataBean.getPrice());//出售估值
        svSell4.setRightString(dataBean.getAmount());//出售数量
        svSell6.setRightString(dataBean.getTotal_price());//总估值
        svSell8.setRightString(dataBean.getCreated_at());//创建时间
        svSell9.setRightString(dataBean.getPay_at());//上传凭证时间
        svSell10.setRightString(dataBean.getFinish_time());//确认订单时间
        svSell12.setRightString(dataBean.getTruename());//买方账号
        svSell13.setRightString(dataBean.getPhone());//手机号码
        imageAdapter.addData(dataBean.getTradepic());
    }

    @Override
    public void vGetSureSuccess(String dataBean) {
        toastNotifyShort(dataBean);
    }
}
